/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Associatedsequence;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Curatorialelement;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Occurrenceelement;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class AssociatedsequenceJpaController {

    public AssociatedsequenceJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Associatedsequence save(Associatedsequence associatedsequence)  {
        Associatedsequence aux = findAssociatedsequence(associatedsequence.getAssociatedsequence());
        if(aux==null){
            return create(associatedsequence);
        } else
            return aux;
    }

    public Associatedsequence create(Associatedsequence associatedsequence) {
        Associatedsequence r = null;
        if (associatedsequence.getCuratorialelementList() == null) {
            associatedsequence.setCuratorialelementList(new ArrayList<Curatorialelement>());
        }
        if (associatedsequence.getOccurrenceelementList() == null) {
            associatedsequence.setOccurrenceelementList(new ArrayList<Occurrenceelement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Curatorialelement> attachedCuratorialelementList = new ArrayList<Curatorialelement>();
            for (Curatorialelement curatorialelementListCuratorialelementToAttach : associatedsequence.getCuratorialelementList()) {
                curatorialelementListCuratorialelementToAttach = em.getReference(curatorialelementListCuratorialelementToAttach.getClass(), curatorialelementListCuratorialelementToAttach.getIdcuratorialelement());
                attachedCuratorialelementList.add(curatorialelementListCuratorialelementToAttach);
            }
            associatedsequence.setCuratorialelementList(attachedCuratorialelementList);
            List<Occurrenceelement> attachedOccurrenceelementList = new ArrayList<Occurrenceelement>();
            for (Occurrenceelement occurrenceelementListOccurrenceelementToAttach : associatedsequence.getOccurrenceelementList()) {
                occurrenceelementListOccurrenceelementToAttach = em.getReference(occurrenceelementListOccurrenceelementToAttach.getClass(), occurrenceelementListOccurrenceelementToAttach.getIdoccurrenceelement());
                attachedOccurrenceelementList.add(occurrenceelementListOccurrenceelementToAttach);
            }
            associatedsequence.setOccurrenceelementList(attachedOccurrenceelementList);
            r = em.merge(associatedsequence);
            for (Curatorialelement curatorialelementListCuratorialelement : associatedsequence.getCuratorialelementList()) {
                curatorialelementListCuratorialelement.getAssociatedsequenceList().add(associatedsequence);
                curatorialelementListCuratorialelement = em.merge(curatorialelementListCuratorialelement);
            }
            for (Occurrenceelement occurrenceelementListOccurrenceelement : associatedsequence.getOccurrenceelementList()) {
                occurrenceelementListOccurrenceelement.getAssociatedsequenceList().add(associatedsequence);
                occurrenceelementListOccurrenceelement = em.merge(occurrenceelementListOccurrenceelement);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return r;
    }

    public void edit(Associatedsequence associatedsequence) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Associatedsequence persistentAssociatedsequence = em.find(Associatedsequence.class, associatedsequence.getIdassociatedsequence());
            List<Curatorialelement> curatorialelementListOld = persistentAssociatedsequence.getCuratorialelementList();
            List<Curatorialelement> curatorialelementListNew = associatedsequence.getCuratorialelementList();
            List<Occurrenceelement> occurrenceelementListOld = persistentAssociatedsequence.getOccurrenceelementList();
            List<Occurrenceelement> occurrenceelementListNew = associatedsequence.getOccurrenceelementList();
            List<Curatorialelement> attachedCuratorialelementListNew = new ArrayList<Curatorialelement>();
            for (Curatorialelement curatorialelementListNewCuratorialelementToAttach : curatorialelementListNew) {
                curatorialelementListNewCuratorialelementToAttach = em.getReference(curatorialelementListNewCuratorialelementToAttach.getClass(), curatorialelementListNewCuratorialelementToAttach.getIdcuratorialelement());
                attachedCuratorialelementListNew.add(curatorialelementListNewCuratorialelementToAttach);
            }
            curatorialelementListNew = attachedCuratorialelementListNew;
            associatedsequence.setCuratorialelementList(curatorialelementListNew);
            List<Occurrenceelement> attachedOccurrenceelementListNew = new ArrayList<Occurrenceelement>();
            for (Occurrenceelement occurrenceelementListNewOccurrenceelementToAttach : occurrenceelementListNew) {
                occurrenceelementListNewOccurrenceelementToAttach = em.getReference(occurrenceelementListNewOccurrenceelementToAttach.getClass(), occurrenceelementListNewOccurrenceelementToAttach.getIdoccurrenceelement());
                attachedOccurrenceelementListNew.add(occurrenceelementListNewOccurrenceelementToAttach);
            }
            occurrenceelementListNew = attachedOccurrenceelementListNew;
            associatedsequence.setOccurrenceelementList(occurrenceelementListNew);
            associatedsequence = em.merge(associatedsequence);
            for (Curatorialelement curatorialelementListOldCuratorialelement : curatorialelementListOld) {
                if (!curatorialelementListNew.contains(curatorialelementListOldCuratorialelement)) {
                    curatorialelementListOldCuratorialelement.getAssociatedsequenceList().remove(associatedsequence);
                    curatorialelementListOldCuratorialelement = em.merge(curatorialelementListOldCuratorialelement);
                }
            }
            for (Curatorialelement curatorialelementListNewCuratorialelement : curatorialelementListNew) {
                if (!curatorialelementListOld.contains(curatorialelementListNewCuratorialelement)) {
                    curatorialelementListNewCuratorialelement.getAssociatedsequenceList().add(associatedsequence);
                    curatorialelementListNewCuratorialelement = em.merge(curatorialelementListNewCuratorialelement);
                }
            }
            for (Occurrenceelement occurrenceelementListOldOccurrenceelement : occurrenceelementListOld) {
                if (!occurrenceelementListNew.contains(occurrenceelementListOldOccurrenceelement)) {
                    occurrenceelementListOldOccurrenceelement.getAssociatedsequenceList().remove(associatedsequence);
                    occurrenceelementListOldOccurrenceelement = em.merge(occurrenceelementListOldOccurrenceelement);
                }
            }
            for (Occurrenceelement occurrenceelementListNewOccurrenceelement : occurrenceelementListNew) {
                if (!occurrenceelementListOld.contains(occurrenceelementListNewOccurrenceelement)) {
                    occurrenceelementListNewOccurrenceelement.getAssociatedsequenceList().add(associatedsequence);
                    occurrenceelementListNewOccurrenceelement = em.merge(occurrenceelementListNewOccurrenceelement);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = associatedsequence.getIdassociatedsequence();
                if (findAssociatedsequence(id) == null) {
                    throw new NonexistentEntityException("The associatedsequence with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Associatedsequence associatedsequence;
            try {
                associatedsequence = em.getReference(Associatedsequence.class, id);
                associatedsequence.getIdassociatedsequence();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The associatedsequence with id " + id + " no longer exists.", enfe);
            }
            List<Curatorialelement> curatorialelementList = associatedsequence.getCuratorialelementList();
            for (Curatorialelement curatorialelementListCuratorialelement : curatorialelementList) {
                curatorialelementListCuratorialelement.getAssociatedsequenceList().remove(associatedsequence);
                curatorialelementListCuratorialelement = em.merge(curatorialelementListCuratorialelement);
            }
            List<Occurrenceelement> occurrenceelementList = associatedsequence.getOccurrenceelementList();
            for (Occurrenceelement occurrenceelementListOccurrenceelement : occurrenceelementList) {
                occurrenceelementListOccurrenceelement.getAssociatedsequenceList().remove(associatedsequence);
                occurrenceelementListOccurrenceelement = em.merge(occurrenceelementListOccurrenceelement);
            }
            em.remove(associatedsequence);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Associatedsequence> findAssociatedsequenceEntities() {
        return findAssociatedsequenceEntities(true, -1, -1);
    }

    public List<Associatedsequence> findAssociatedsequenceEntities(int maxResults, int firstResult) {
        return findAssociatedsequenceEntities(false, maxResults, firstResult);
    }

    private List<Associatedsequence> findAssociatedsequenceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Associatedsequence.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Associatedsequence findAssociatedsequence(String q) {
        Query query= getEntityManager().createNamedQuery("Associatedsequence.findByAssociatedsequence");
        query.setParameter("associatedsequence",q);

        return query.getResultList().isEmpty()?null:(Associatedsequence)query.getResultList().get(0);
    }
    public Associatedsequence findAssociatedsequence(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Associatedsequence.class, id);
        } finally {
            em.close();
        }
    }

    public int getAssociatedsequenceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Associatedsequence> rt = cq.from(Associatedsequence.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
